package cn.cvs.servlet;

import cn.cvs.dao.FruitMapper;
import cn.cvs.pojo.Resume;
import cn.cvs.utils.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(urlPatterns = "/ResumeInsertServlet")
public class InsertServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        SqlSession sqlSession = null;
        int count = 0;
        try {
            sqlSession = MyBatisUtil.createSqlSession();
            Resume resume = new Resume();
            resume.setUserName(request.getParameter("userName"));
            resume.setSex(request.getParameter("sex"));
            resume.setBirth(request.getParameter("birth"));
            resume.setAddress(request.getParameter("address"));
            resume.setTelephone(request.getParameter("telephone"));
            resume.setSchool(request.getParameter("school"));
            resume.setStatus(Integer.parseInt(request.getParameter("status")));
            count = sqlSession.getMapper(FruitMapper.class).ResumeInsert(resume);
        } catch (Exception e) {
            e.printStackTrace();
            sqlSession.rollback();
            count = 0;
        } finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        PrintWriter out = response.getWriter();
        if (count > 0) {
            out.print("<script>alert('添加成功');location.href='ResumeSelectAndViewSexServlet';</script>");
        } else {
            out.print("<script>alert('添加失败');location.href='ResumeSelectAndViewSexServlet';</script>");

        }
        out.flush();
        out.close();
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doGet(req, resp);
    }
}
